package com.wizz.trade.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wizz.trade.auth.dto.SysRoleDto;
import com.wizz.trade.auth.dto.page.SysRolePageDto;
import com.wizz.trade.auth.entity.SysMenu;
import com.wizz.trade.auth.entity.SysRole;
import com.wizz.trade.auth.mapper.SysRoleMapper;
import com.wizz.trade.auth.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.auth.vo.SysMenuVo;
import com.wizz.trade.auth.vo.SysRoleVo;
import com.wizz.trade.common.ServiceException;
import com.wizz.trade.util.PageUtil;
import com.wizz.trade.common.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-04-02 04:26:06
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public void insert(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }

    @Override
    public void update(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }

    @Override
    public void delete(Long id) {
        sysRoleMapper.logicDelete(id);
    }

    @Override
    public void changeStatus(Long id) {
        sysRoleMapper.changeStatus(id);
    }

    @Override
    public PageVo<SysRole> getPage(SysRolePageDto sysRoleDto) {
        IPage<SysRole> page = sysRoleMapper.getPage(PageUtil.initPage(sysRoleDto), sysRoleDto);
        return new PageVo<>(page);
    }

    @Override
    public List<SysRoleVo> getRolesVoList(Long userId) {
        return sysRoleMapper.getRoleVoList(userId);
    }

    @Override
    public void bindMenu(Long roleId, List<Long> menus) {
        sysRoleMapper.clearMenu(roleId);
        sysRoleMapper.bindMenu(roleId, menus);
    }

    @Override
    public List<SysMenuVo> getMenusByRoleId(Long id) {
        List<SysMenu> menus = sysRoleMapper.getMenusByRoleId(id);
        ArrayList<SysMenuVo> resList = new ArrayList<>();
        for (SysMenu menu : menus) {
            SysMenuVo cur = new SysMenuVo();
            BeanUtils.copyProperties(menu, cur);
            resList.add(cur);
        }
        return resList;

    }

}
